package cn.tedu.yushougeproduct.controller;

import cn.tedu.yushougeproduct.pojo.param.OrdersAddNewParam;
import cn.tedu.yushougeproduct.pojo.vo.OrdersListItemVO;
import cn.tedu.yushougeproduct.pojo.vo.OrdersStandardVO;
import cn.tedu.yushouge.commons.pojo.vo.PageData;
import cn.tedu.yushougeproduct.service.IOrdersService;
import cn.tedu.yushouge.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/orders/")
@Slf4j
@Api(tags = "03：订单管理模块")
@Validated
public class OrdersController {
    @Autowired
    IOrdersService iOrdersService;

    @PostMapping("delete")
    @ApiOperation("根据ID删除订单")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@Range(min = 1, message = "根据ID删除订单失败，请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【根据ID删除订单】的请求，参数：{}", id);
        iOrdersService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("updateState")
    @ApiOperation("根据ID更新订单状态")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单Id", required = true, dataType = "long"),
            @ApiImplicitParam(name = "stateId", value = "状态ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateState(@Range(min = 1, message = "根据ID删除订单失败，请提交合法的ID值！")
                                        @RequestParam Long orderId,@RequestParam Long stateId) {
        log.debug("开始处理【根据ID修改购物车状态】的请求，参数：{}", orderId);
        iOrdersService.updateOrderState(orderId,stateId);
        return JsonResult.ok();
    }

    @GetMapping("standard")
    @ApiOperation("根据ID查询订单详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "long")
    })
    public JsonResult<OrdersStandardVO> getStandardById(@Range(min = 1, message = "根据ID查询订单详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) throws Exception {
        log.debug("开始处理【根据ID查询订单详情】的请求，参数：{}", id);
        OrdersStandardVO result = iOrdersService.getStandardById(id);
        return JsonResult.ok(result);
    }

    @GetMapping("list")
    @ApiOperation("查询订单")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query")
    })
    public JsonResult<PageData<OrdersListItemVO>> list(@Range(min = 1, message = "查询订单失败，请提供正确的页码值！") Integer pageNum) {
        log.debug("开始处理【查询订单列表】的请求，页码：{}", pageNum);
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        PageData<OrdersListItemVO> pageData = iOrdersService.list(pageNum);
        return JsonResult.ok(pageData);
    }

    @GetMapping("list-by-userId")
    @ApiOperation("根据用户id查询订单")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query")
    })
    public JsonResult<List<OrdersListItemVO>> list(@Range(min = 1, message = "查询订单失败，请提供正确的用户id！") Long userId) {
        log.debug("开始处理【查询订单列表】的请求，页码：{}", userId);
        List<OrdersListItemVO> pageData = iOrdersService.listByUserId(userId);
        return JsonResult.ok(pageData);
    }
}
